import axios from 'axios'
import Vue from 'vue'
import Router from 'vue-router'
const Layout = () => import('@/components/Layout')
const NotFound = () => import('@/components/404.vue')
const WelCome = () => import('@/components/welCome.vue')
const Login = () => import('@/view/login')
const Home = () => import('@/view/home')
const Detail = () => import('@/view/detail')

// 不是基于Vue的插件，如果想绑定到Vue上面
Vue.prototype.$axios = axios
// axios.defaults.baseURL = "http://39.108.135.214:8899/"
// 在跨域的时候，允许访问服务器带上cookies
axios.defaults.withCredentials = true

Vue.use(Router)
// layout公共的一级路由
const router = new Router({
  routes: [
    {path: '/', redirect: '/welcome'},
    {
      path: '/',
      name: 'layout',
      component: Layout
    },
    {
      path: '/404',
      component: NotFound,
      name: '',
      hidden: true
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/',
      component: Layout,
      name: '概述',
      iconClass: 'el-icon-menu', // 侧边图样class
      hidden: false,
      meta: {hasAuth: true},
      children: [
        {path: '/welcome', component: WelCome, name: '主页', hidden: true, meta: {hasAuth: true}}
      ]
    },
    {
      path: '/',
      component: Layout,
      name: '首页',
      iconClass: 'el-icon-message', // 侧边图样class
      hidden: false,
      meta: {hasAuth: true},
      children: [
        {path: '/home', component: Home, name: '主页', hidden: true, meta: {hasAuth: true}},
        {path: '/detail', component: Detail, name: '详情页', hidden: true, meta: {hasAuth: true}}
      ]
    }
  ]
})
/**
 * 路由守卫
 *
 * to 跳转到哪里去
 * from 从哪里跳转过来
 * next 调用next就会触发路由，调用它，就不会触发路由
 */
router.beforeEach((to, from, next) => {
  if (to.meta.hasAuth) {
    next()
    // router.push({name: 'login'})
  } else {
    next()
  }
})

export default router
